﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

using NBearLite;
using Xin478.CSWork.Common;
using Xin478.CSWork.DomainModel;
using Xin478.MySocket;

namespace Xin478.CSWork.Server
{
    public class Task : ResponseBase
    {
        public void 同步数量(NetEventArgs e, int lastId)
        {
            int userid = this.correspondent.OnlineUser.FindUserId(e.Client);
            int count= userid==-1 ? 0 : Databases.MyWork.Select(MyWork.Works, MyWork.Works.Id.Count()).Where(MyWork.Works.Id > lastId && (MyWork.Works.UserId == userid || MyWork.Works.PublisherId == userid)).ToScalar<int>();

            this.correspondent.Send(e, "任务同步数量", count);
        }

        public void 同步列表(NetEventArgs e, int afterWorkId, int pageSize)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);
            IList<WorkInfo> wis = null;
            if (userId != -1)
            {
                wis = WorkModel.GetSycList(Databases.MyWork, userId, afterWorkId, pageSize);
            }

            this.correspondent.Send(e, "任务同步列表", afterWorkId, wis);
        }

        public void 文件下载(NetEventArgs e, int fileId)
        {
            WorkFileInfo fileInfo = WorkModel.GetOneFile(Databases.MyWork, fileId);
            if (fileInfo != null)
            {
                string filePath =AppSetting.Default.文件保存目录 + fileInfo.FileDir + fileInfo.FileName;
                try
                {
                    this.correspondent.TransferFileHelper.AddTransferFile(filePath, fileId.ToString(), e.Client);
                }
                catch { }
            }
        }

        public void 任务信息(NetEventArgs e, int workId)
        {
            this.correspondent.Send(e, "任务信息", WorkModel.GetInfo(Databases.MyWork, workId));
        }

        public void 任务详细(NetEventArgs e, int workId)
        {
            this.correspondent.Send(e, "任务详细", WorkModel.GetOne(Databases.MyWork, workId));
        }

        public void 要求列表(NetEventArgs e, int workId)
        {
            this.correspondent.Send(e, "任务要求列表", workId, Databases.MyWork.Select(MyWork.WorkCommands).Where(MyWork.WorkCommands.Id == workId).ToList<WorkCommandInfo>());
        }

        public void 文件列表(NetEventArgs e, int workId)
        {
            this.correspondent.Send(e, "任务文件列表", workId, WorkModel.GetOneFiles(Databases.MyWork, workId));
        }

        public void 任务数量(NetEventArgs e, WorkStatusFlag status)
        {
            int userid = this.correspondent.OnlineUser.FindUserId(e.Client);

            int count = userid == -1 ? 0 : Databases.MyWork.Select(MyWork.Works, MyWork.Works.Id.Count()).Where(ConvertStatus(status,userid)).ToScalar<int>();

            this.correspondent.Send(e, "任务数量", count, status);
        }

        public void 任务列表(NetEventArgs e, WorkStatusFlag status, int start, int pageSize)
        {
            int userid = this.correspondent.OnlineUser.FindUserId(e.Client);
            if(userid==-1) return;

            this.correspondent.Send(e, "任务列表",start, WorkModel.GetTable(Databases.MyWork, start, pageSize, ConvertStatus(status, userid), MyWork.Works.Id, false)); 
        }

        static WhereClip ConvertStatus(WorkStatusFlag status, int userid)
        {
            WhereClip where = null;
            switch (status)
            {
                case WorkStatusFlag.MyProcessing:
                    where = MyWork.Works.UserId == userid && MyWork.Works.FinishTime == null;
                    break;
                case WorkStatusFlag.MyFinished:
                    where = MyWork.Works.UserId == userid && MyWork.Works.FinishTime != null;
                    break;
                case WorkStatusFlag.SomeOneWaitAccept:
                    where = MyWork.Works.PublisherId == userid && MyWork.Works.AcceptTime == null && MyWork.Works.FinishTime == null;
                    break;
                case WorkStatusFlag.SomeOneProcessing:
                    where = MyWork.Works.PublisherId == userid && MyWork.Works.AcceptTime != null && MyWork.Works.FinishTime == null;
                    break;
                case WorkStatusFlag.SomeOneFinished:
                    where = MyWork.Works.PublisherId == userid && MyWork.Works.FinishTime != null;
                    break;
            }
            return where;
        }
    }
}
